const app = getApp();
const AUTH = require('../../utils/auth')
import { mpId } from "../../utils/config";
import {
  getCustBaseByUserLoginState
} from "../../api/custBaseController";
import {
  updateUserInfo
} from "../../api/miniProgramController";
Component({
	options: {
	  addGlobalClass: true
	},
	properties: {
		isHidden: {
			type: Boolean,
			value: true,
		}
	},
	lifetimes: {
		attached() {
			this.checkToken()

		}
	},
	methods: {
		checkToken() {
			const token = wx.getStorageSync('custId');
			if (!token) return false
			// WXAPI.checkToken(token).then(res => {
			// 	if (res.code != 0) {
			// 		wx.removeStorageSync('token')
			// 		this.login();
			// 	} else {
			// 		// wx.showToast({
			// 		// 	title:'登录成功',
			// 		// 	icon:'success'
			// 		// })
			// 		app.globalData.token = token
			// 		this.triggerEvent('afterAuth', token)
			// 	}
			// })
			return true;
		},
		close() {
			this.setData({
				isHidden: true,
			})
			this.triggerEvent('closeAuth')
		},
		    /**
     * 更新头像昵称
     */
    async updateUserInfo(res) {
      let self = this
      let resolve = await updateUserInfo({
        res: JSON.stringify(res),
        mpId: mpId
      })
      if (resolve.success) {
        self.getCustBaseByUserLoginState()
      }
		},
		/**
     * 根据userLoginState获取当前客户
     */
    async getCustBaseByUserLoginState() {
      let resolve = await getCustBaseByUserLoginState()
      if (resolve.success) {
        let userInfo = {
          "nickName": resolve.data.nickname,
          "gender": resolve.data.sex,
          "language": resolve.data.language,
          "city": resolve.data.city,
          "province": resolve.data.province,
          "country": resolve.data.country,
          "avatarUrl": resolve.data.headImgUrl,
          "vipType": resolve.data.vipType,
          "id": resolve.data.id
        }
        wx.setStorageSync('userInfo', userInfo)
        wx.setStorageSync('custId', resolve.data.id)
				wx.setStorageSync('userBase', resolve.data)
				this.setData({
          userInfo: userInfo
				})
				var pages = getCurrentPages();
				var Page = pages[pages.length - 1];//当前页
				let wxlogin = resolve.data.id!=null || resolve.data.id!='' ?true:false
				Page.setData({
					wxlogin: wxlogin
				})
				wx.navigateTo({
					url: '/pages/registration/index',
				})
      }
    },
		bindGetUserInfo() {
			let self = this
			let userInfo = wx.getStorageSync('userBase').nickname
			if (!userInfo) {
				wx.getUserProfile({
					desc: "获取你的昵称、头像、地区及性别",
					success: res => {
						self.updateUserInfo(res.userInfo)
					},
					fail: res => {
						//拒绝授权
						wx.showToast({
							title: '您拒绝了授权',
							icon: 'none'
						})
						return;
					}
				})
			}else{
				wx.navigateTo({
					url: '/pages/registration/index',
				})
			}

		},
		login() {
			const token = wx.getStorageSync('custId');
			this.triggerEvent('afterAuth', token)
		},
		registerUser: function() {
			let that = this;
			wx.login({
				success: function(res) {
					let code = res.code; // 微信登录接口返回的 code 参数，下面注册接口需要用到
					wx.getUserInfo({
						success: function(res) {
							let iv = res.iv;
							let encryptedData = res.encryptedData;
							let referrer = '' // 推荐人
							let referrer_storge = wx.getStorageSync('referrer');
							if (referrer_storge) {
								referrer = referrer_storge;
							}
							// 下面开始调用注册接口
							// WXAPI.register_complex({
							// 	code: code,
							// 	encryptedData: encryptedData,
							// 	iv: iv,
							// 	referrer: referrer
							// }).then(function(res) {
							// 	wx.hideLoading();
							// 	that.login();
							// })
						}
					})
				}
			})
		}
	}
})
